package com.commerce.controller.back.admin;

import com.commerce.authority.AuthType;
import com.commerce.authority.Authority;
import com.commerce.common.JsonResult;
import com.commerce.common.Reminder;
import com.commerce.constant.UserType;
import com.commerce.model.Admin;
import com.commerce.model.Role;
import com.commerce.page.AdminPage;
import com.commerce.page.RolePage;
import com.commerce.service.AdminService;
import com.commerce.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wyj
 * @Title:
 * @Description:
 */
@Controller
@RequestMapping(value = "/admin/adminuser",method = RequestMethod.POST)
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    /**
     * 管理员列表
     * @return
     */
    @RequestMapping("list")
    @Authority(AuthType.ADMIN_SELECT)
    public String list(){
        return "back/admin/list";
    }

    /**
     * 分页查询
     * @param adminPage
     * @return
     */
    @ResponseBody
    @RequestMapping("pageList")
    @Authority(AuthType.ADMIN_SELECT)
    public JsonResult pageList(AdminPage adminPage){
        adminService.selectByPage(adminPage);
        return JsonResult.returnJsonResult("0","",""+adminPage.getTotal(),adminPage.getList());
    }

    /**
     * 编辑页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("info")
    @Authority({AuthType.ADMIN_UPDATE,AuthType.ADMIN_INSERT})
    public String info(Integer id , Model model){
        Admin admin = null;
        if(id != null)
            admin=adminService.selectById(id);
        model.addAttribute("admin",admin);
        model.addAttribute("role",roleService.selectByPage(new RolePage()).getList());
        return "back/admin/info";
    }

    /**
     * 新增
     * @param admin
     * @return
     */
    @ResponseBody
    @RequestMapping("addAdmin")
    @Authority(AuthType.ADMIN_INSERT)
    public String addAdmin(Admin admin , HttpServletRequest request){
        return adminService.insert(admin,request);
    }

    /**
     * 修改
     * @param admin
     * @return
     */
    @ResponseBody
    @RequestMapping("editAdmin")
    @Authority(AuthType.ADMIN_UPDATE)
    public String editAdmin(Admin admin , HttpServletRequest request){
        return adminService.update(admin,request);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delAdmin")
    @Authority(AuthType.ADMIN_DELETE)
    public String delAdmin(int id){
        return adminService.deleteById(id);
    }

    /**
     * 查询账号名
     * @param account
     * @return
     */
    @ResponseBody
    @RequestMapping("selectByAccount")
    public String selectByAccount(String account){
        return adminService.selectByAccount(account);
    }

    /**
     * 开启或关闭账号
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/openAdmin")
    public String openAdmin(int id){
        return adminService.updateByOpen(id);
    }

    /**
     *用户资料页面
     * @return
     */
    @RequestMapping("/userDataPage")
    public String userDataPage(Model model,HttpSession session){
        Admin admin = (Admin) session.getAttribute(UserType.ADMINUSET.name());
        List<Role> list = null;
        if(admin.getRole_ids()==null || admin.getRole_ids().equals("")){
            Role role = new Role();
            role.setName("超级管理员");
            list = new ArrayList<>();
            list.add(role);
        }else{
            list = roleService.selectByRoleIds(admin.getRole_ids());
        }
        model.addAttribute("role",list);
        return "back/admin/userData";
    }

    /**
     *修改个人资料
     * @param admin
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/userData")
    public String userData(Admin admin , HttpServletRequest request){
       Admin useradmin =  (Admin)request.getSession().getAttribute(UserType.ADMINUSET.name());
       useradmin.setName(admin.getName());
       useradmin.setImgurl(admin.getImgurl());
       String hint = adminService.update(useradmin,request);
       if(hint.equals(Reminder.success.name())){
           request.getSession().setAttribute(UserType.ADMINUSET.name(),useradmin);
       }
       return hint;
    }
    /**
     *用户修改密码页面
     * @return
     */
    @RequestMapping("/updatePwdPage")
    public String updatePwdPage(){
        return "back/admin/updatePwd";
    }

    /**
     *
     * @param pwd
     * @param pwdnew
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("updatePwd")
    public String updatePwd(String password ,String pwd ,String pwdnew , HttpSession session){
        return adminService.updatePwd(password,pwd,pwdnew,session);
    }
}
